﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.DrawingCore.Imaging;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;

namespace HT.Future.Common
{
    public sealed class UtilHelper
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="message"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static byte[] CreateQrcode(string message, int width = 400, int height = 400)
        {
            int heig = width;
            if (width > height)
            {
                heig = height;
                width = height;
            }
            if (string.IsNullOrWhiteSpace(message))
            {
                return null;
            }
            var w = new ZXing.QrCode.QRCodeWriter();

            var b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
            var zzb = new ZXing.ZKWeb.BarcodeWriter
            {
                Options = new EncodingOptions { Margin = 0 }
            };
            var bmp = zzb.Write(b);
            byte[] byteArray = null;
            using (var stream = new MemoryStream())
            {
                bmp.Save(stream, ImageFormat.Png);
                byteArray = stream.GetBuffer();
            }
            bmp.Dispose();
            return byteArray;
        }

        /// <summary>
        /// 生成条形码
        /// </summary>
        /// <param name="message"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static byte[] CreateBarcode(string message, int width = 500, int height = 120)
        {
            var w = new ZXing.OneD.CodaBarWriter();
            var b = w.encode(message, BarcodeFormat.CODABAR, width, height);
            var zzb = new ZXing.ZKWeb.BarcodeWriter
            {
                Options = new EncodingOptions()
                {
                    Margin = 3,
                    PureBarcode = true
                }
            };
            var bmp = zzb.Write(b);
            byte[] byteArray = null;
            using (var stream = new MemoryStream())
            {
                bmp.Save(stream, ImageFormat.Png);
                byteArray = stream.GetBuffer();
            }
            bmp.Dispose();
            return byteArray;
        }

        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string RandomNumber(int len = 6)
        {
            var arrChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            var num = new StringBuilder();
            var rnd = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < len; i++)
            {
                num.Append(arrChar[rnd.Next(0, 9)].ToString());
            }
            return num.ToString();
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="content">请求参数</param>
        /// <param name="method">方法</param>
        /// <returns></returns>
        public static async Task<string> RequestAsync(string url, object content = null, string method = "post")
        {
            method = method.ToLower();
            HttpResponseMessage result;
            using (var client = new HttpClient())
            {
                HttpContent body = null;
                if (content != null)
                {
                    if (content is string)
                    {
                        body = new StringContent(content.ToString());
                    }
                    else
                    {
                        var data = JsonConvert.SerializeObject(content);
                        body = new StringContent(data, Encoding.UTF8, "application/json");
                    }
                    //client.DefaultRequestHeaders.Add("Content-Type", "application/json");
                    //body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                }
                switch (method)
                {
                    case "get":
                        result = await client.GetAsync(url);
                        break;
                    case "post":
                        result = await client.PostAsync(url, body);
                        break;
                    case "put":
                        result = await client.PutAsync(url, body);
                        break;
                    case "delete":
                        result = await client.DeleteAsync(url);
                        break;
                    default:
                        throw new Exception($"不支持方法{method}");
                }
                if (body != null)
                {
                    body.Dispose();
                }
            }
            result.EnsureSuccessStatusCode();
            return await result.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        public static void CreateDirectory(string path)
        {
            if (Directory.Exists(path)) return;
            Directory.CreateDirectory(path);
        }

    }
}
